
#include <string>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <cstring>
 #include <arpa/inet.h>
 #include <iostream>

 using namespace std;

int main(int argc, char** argv)
{
    if(argc!= 3) return -1;
    struct sockaddr_in server;
    memset(&server, 0, sizeof(server));
    server.sin_family= AF_INET;
    server.sin_addr.s_addr= inet_addr(argv[1]);
    server.sin_port= htons(stoi(argv[2]));   //BUG--没有转成网络字节序

    int sockfd= socket(AF_INET, SOCK_DGRAM, 0);

    string s;
    char buffer[1024];
    while(1)
    {
        cout<<"请输入英文：";
        getline(cin, s);
        
        int n= sendto(sockfd, s.c_str(), s.size(), 0, (struct sockaddr*)&server, sizeof(server));
        if(n<0)
        {
            cout<<"发送失败"<<endl;
        }

        cout<<"对应中文是：";
        n= recvfrom(sockfd, buffer, sizeof(buffer)-1, 0, nullptr,0);
        buffer[n]=0;
        cout<<buffer<<endl;
    }


    return 0;
}